package com.lj.exam.service.impl;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.lj.exam.dal.daointerface.SmsSendDAO;
import com.lj.exam.dal.daointerface.SmsTplDAO;
import com.lj.exam.dal.dataobject.SmsSendDO;
import com.lj.exam.dal.dataobject.SmsTplDO;
import com.lj.exam.model.SmsSendRequest;
import com.lj.exam.model.SmsSendResponse;
import com.lj.exam.service.SmsSendService;
import com.lj.exam.util.DateTimeUtils;

public class SmsSendServiceImpl implements SmsSendService{
	
	@Autowired
	private SmsTplDAO smsTplDAO;
	@Autowired
	private SmsSendDAO smsSendDAO;
	private int pools =10;
	
	public SmsSendResponse sendSms(SmsSendRequest req)  {
		
		SmsSendResponse rsp=new SmsSendResponse();
		
		//根据短信编号 获取短信模板
		SmsTplDO smstpl = smsTplDAO.selectModelName(req.getSmsCode());
		System.out.println(smstpl.getTplContent());
		String smstplValue = smstpl.getTplContent();
	    String smsValue = req.getSmsContents();
		//根据短信模板和请求短信参数，组装 短信内容
		String smsSendContent=genSmsSendContent(smsValue,smstplValue);
		System.out.println(smsSendContent);

		SmsSendDO smsSendDO = getSmsSendInfo(req,smstpl);
		//短信流水表
		smsSendDAO.insert(smsSendDO);
		rsp.setMobile(req.getMobile());
		rsp.setSmsCode("0000");
	    rsp.setSmsContent("ok"); 
	    return rsp;
	}
	private String genSmsSendContent(String param,String smstplValue) {
		
		List<String> params=Arrays.asList(param.split(",", -1));
		int repLen=smstplValue.split("%", -1).length-1;
		
		String sendVal=smstplValue;
		for(String paramVal:params){
			sendVal=sendVal.replaceFirst("%", paramVal);
		}
		return sendVal;
		
	}
	private SmsSendDO getSmsSendInfo(SmsSendRequest request, SmsTplDO smstpl) {
		SmsSendDO smsSendDO = new SmsSendDO();
		
		String seqNo=DateTimeUtils.formatLongFormat(new Date());
		
		int kindCode=Integer.parseInt(seqNo.substring(seqNo.length()-2));
		kindCode=kindCode%pools;
		Date recvDate=new Date();
		Calendar cal = Calendar.getInstance(); 
		cal.setTime(recvDate);
		
		smsSendDO.setId(seqNo);
		smsSendDO.setUserId(request.getUserId());
		smsSendDO.setUserName(request.getUserName());
		smsSendDO.setCreateTime(recvDate);
		smsSendDO.setModifiedTime(recvDate);
		
		cal.add(Calendar.MINUTE, 10);
		
		smsSendDO.setRecvTime(cal.getTime());
		smsSendDO.setDay(cal.get(Calendar.DAY_OF_MONTH));
		smsSendDO.setEndTime(smstpl.getEndTime());
		smsSendDO.setExpiredTime(smstpl.getExpiredTime());
		smsSendDO.setStartTime(smstpl.getStartTime());
		//,,,,
	    //,,,,, ,,
	    //,send_time,,,,,,resp_code,resp_msg
		smsSendDO.setValidTime(smstpl.getValidTime());
		smsSendDO.setCreator(request.getFromSys());
		smsSendDO.setModifier(request.getFromSys());
		smsSendDO.setMobile(request.getMobile());
		//smsSendDO.setUserId(request.getUserId());
		
		//smsSendDO.setUserName(request.getUserName());
		smsSendDO.setKindCode(kindCode);
		smsSendDO.setSmsContent(request.getSmsSendContent());
		smsSendDO.setSmsCode(request.getSmsCode());
		//I 初始化, P 处理中, F 处理失败, S 处理成功
		smsSendDO.setStatus("I");
		smsSendDO.setSourceCode(request.getSourceCode());
		smsSendDO.setPriority(request.getPriority());
		//待修改项
		smsSendDO.setSendTime(new Date(2017-05-05));
		smsSendDO.setRespCode("0000");
		smsSendDO.setRespMsg("ok");
		return smsSendDO;
	}

	public SmsSendResponse sendSmsExt(SmsSendRequest req) {
		// TODO Auto-generated method stub
		return null;
	}

}
